package cnu.ldc.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;

import cnu.ldc.dao.HobbyJdbcDaoImpl;
import cnu.ldc.dao.UserJdbcDaoImpl;
import cnu.ldc.dao.iface.UserDao;
import cnu.ldc.doman.Hobby;
import cnu.ldc.doman.NativePlace;
import cnu.ldc.doman.User;
import cnu.ldc.doman.UserDetail;
import cnu.ldc.service.HobbyServiceImpl;
import cnu.ldc.service.NativePlaceServiceImpl;
import cnu.ldc.service.UserserviceImpl;
import cnu.ldc.service.iface.HobbyService;
import cnu.ldc.service.iface.NativePlaceService;
import cnu.ldc.service.iface.Userservice;



@WebServlet("/login.Servlet")
public class loginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       HobbyService hobbyservice=new HobbyServiceImpl();
       private Userservice userService=new UserserviceImpl();
       NativePlaceService nativeplaceservice=new NativePlaceServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 String name=request.getParameter("username");
	 String pass=request.getParameter("password");
	 String nicheng=request.getParameter("nicheng");
	 String sex=null==request.getParameter("sex")?"":request.getParameter("sex");
		String email=request.getParameter("email");
		String cityCode=request.getParameter("cityCode");
		String[] favs=request.getParameterValues("hobbies");
	 String param=request.getParameter("param");
	 response.setCharacterEncoding("UTF-8");
	 response.setContentType("text/html ;charset=UTF-8");
	PrintWriter out=response.getWriter();
	Map<String,Object> map=new HashMap<>();
	String cookie=null==request.getParameter("isCookie")?"no":request.getParameter("isCookie");
	Userservice userservice=new UserserviceImpl();
	if(Objects.equals("checkname",param)) {
		map.put("result","ok");
		map.put("name", name);
		
		out.print(JSONObject.toJSON(map));
		
	}
	if(Objects.equals("login", param)) {
	 if(userservice.login(name, pass)) {
		 if(Objects.equals("yes",cookie)) {
		 Cookie iscookie=new Cookie("web","www.log.com");
		 iscookie.setMaxAge(60*5);
		 response.addCookie(iscookie);
		
		 }
			HttpSession session=request.getSession();
			session.setAttribute("name", name);
		 request.getRequestDispatcher("views/main.jsp").forward(request, response);
	 }else {
		 request.getRequestDispatcher("login.jsp").forward(request, response);
	 }
	}
	if(Objects.equals("doRegister", param)) {
		User user=new User(0,name,nicheng,pass,sex,email);
		UserDetail ud=new UserDetail(0,cityCode,String.join(",", favs),0);
		if(userService.register(user, ud)) {
			out.print("<script>alert('注册成功，请在登录页面登录。');window.location.href='inlit.servlet'</script>");
		}else {
			out.print("<script>alert('注册失败，请换个时间再试一次。');window.location.href='inlit.servlet'</script>");
		}
	}
	if(Objects.equals("register", param)) {
		List<Hobby> hobby=hobbyservice.getHobbies();
		List<NativePlace> nativeplace=nativeplaceservice.getProvince();
		request.setAttribute("hobby",hobby);
		request.setAttribute("nativeplace",nativeplace);
		request.getRequestDispatcher("register.jsp").forward(request,response);
	}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		doGet(request, response);
	}

}
